package com.validant.enigma3.samples;

import java.util.Iterator;

import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.validant.enigma3.common.Constants;
import com.validant.enigma3.common.TestBase;
import com.validant.enigma3.controls.ExtWebElement;
import com.validant.enigma3.common.Utils;
import com.validant.enigma3.uat.LoginLib;

/*
 * This is a sample test
 */
public class Login extends TestBase {
	
	// for test data
	private static final String VALID_LOGIN_DATA_SHEET = "ValidLogin";
	private static final String LOGIN_VALID_DATA_PROVIDER = "Login Data Provider";
	
	// for validated messages
	private static final String ERR_MSG_REQUIRED_FIELD = "FAILED This field is required";
	
	@DataProvider(name = LOGIN_VALID_DATA_PROVIDER)	
	public Iterator<Object[]> createLoginValidDataProvider(ITestContext context) {
		return Utils.createDataProvider(Utils.getTestParameter(context, Constants.PARAM_TEST_DATA_FILE), VALID_LOGIN_DATA_SHEET);
	}		
	
	@Test(description = "Login with valid user/pass", dataProvider = LOGIN_VALID_DATA_PROVIDER)    
	public void loginValidData(String username, String password)
	{
		// enter data
		LoginLib.enterData(driver, uimap, username, password);
	   
		// verify validation message
		ExtWebElement user= new ExtWebElement(driver.waitForElementVisible(uimap.getElement(LoginLib.CTRL_USER)));
		ExtWebElement pass= new ExtWebElement(driver.waitForElementVisible(uimap.getElement(LoginLib.CTRL_PASS)));

		user.verifyAttributeEquals("data-errorqtip", "");
		pass.verifyAttributeEquals("data-errorqtip", "");
		
		// reset all controls
		driver.refreshPage();
	}
	
	@Test(description = "Login without user/pass")
	public void loginNoData()  
	{
		// enter data
		LoginLib.enterData(driver, uimap, "", "");
	   
		// verify validation message
		ExtWebElement user= new ExtWebElement(driver.waitForElementVisible(uimap.getElement(LoginLib.CTRL_USER)));
		ExtWebElement pass= new ExtWebElement(driver.waitForElementVisible(uimap.getElement(LoginLib.CTRL_PASS)));
		
		user.verifyAttributeContains("data-errorqtip", ERR_MSG_REQUIRED_FIELD);
		pass.verifyAttributeContains("data-errorqtip", ERR_MSG_REQUIRED_FIELD);
		
		// reset all controls
		driver.refreshPage();				
	}
	
}
